package com.atnjupt.java;
/**
 * @author Wizarder
 * @create 2021-05-25 17:35
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Arrays;

public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        System.out.println("用户名：" + request.getParameter("username"));
        System.out.println("密码：" + request.getParameter("password"));
        System.out.println("性别：" + request.getParameter("sex"));
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("兴趣爱好：" + Arrays.asList(hobbies));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数  post请求如果输入中文会出现乱码，怎么解决乱码？调用如下方法
        // 也要在获取请求参数之前调用才有效 如果把获取参数在这个方法之上运行就会出错误
        request.setCharacterEncoding("UTF-8");
        System.out.println("用户名：" + request.getParameter("username"));
        System.out.println("密码：" + request.getParameter("password"));
        System.out.println("性别：" + request.getParameter("sex"));
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("兴趣爱好：" + Arrays.asList(hobbies));
    }
}
